package com.zimi.service;

import com.zimi.mapper.WfModuleMapper;
import com.zimi.model.WfModule;
import com.zimi.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Auther: lmc
 * @Date: 2019/3/27 10:33
 * @Description:
 */
@Service
public class WfModuleService {

    @Autowired
    private WfModuleMapper wfModuleMapper;


    public int deleteByPrimaryKey(Integer id) {
        return wfModuleMapper.deleteByPrimaryKey(id);
    }


    public int insert(WfModule wfModule) {
        return wfModuleMapper.insert(wfModule);
    }


    public int insertSelective(WfModule wfModule) {
        wfModule.setCreateTime(new Date());
        wfModule.setCreateUser(UserUtil.getUser().getId());
        Integer orderNum = wfModuleMapper.selectMaxOrderNum();
        if (orderNum == null) {
            orderNum = 1;
        } else {
            orderNum = orderNum + 1;
        }
        wfModule.setOrderNum(orderNum);
        return wfModuleMapper.insertSelective(wfModule);
    }


    public WfModule selectByPrimaryKey(Integer id) {
        return wfModuleMapper.selectByPrimaryKey(id);
    }


    public int updateByPrimaryKeySelective(WfModule wfModule) {
        return wfModuleMapper.updateByPrimaryKeySelective(wfModule);
    }


    public int updateByPrimaryKey(WfModule wfModule) {
        return wfModuleMapper.updateByPrimaryKey(wfModule);
    }


    public List<WfModule> selectAll(WfModule wfModule) {
        return wfModuleMapper.selectAll(wfModule);
    }

    public String moveUp(int id) {
        WfModule module = wfModuleMapper.selectByPrimaryKey(id);
        WfModule m = new WfModule();
        m.setModuleState(1);
        List<WfModule> list = wfModuleMapper.selectAll(m);

        int index = -1;
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getModuleId() == module.getModuleId() || list.get(i).getModuleId().equals(module.getModuleId())) {
                    index = i;
                }
            }
        }

        if (index == 0) {
            return "已到达顶部";
        } else {
            WfModule module1 = list.get(index - 1);
            int sort = module.getOrderNum();
            int sort1 = module1.getOrderNum();

            module.setOrderNum(sort1);
            wfModuleMapper.updateByPrimaryKeySelective(module);

            module1.setOrderNum(sort);
            wfModuleMapper.updateByPrimaryKeySelective(module1);
        }

        return "上移成功";
    }


    public String moveDown(int id) {
        WfModule module = wfModuleMapper.selectByPrimaryKey(id);
        WfModule m = new WfModule();
        m.setModuleState(1);
        List<WfModule> list = wfModuleMapper.selectAll(m);

        int index = -1;
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getModuleId() == module.getModuleId() || list.get(i).getModuleId().equals(module.getModuleId())) {
                    index = i;
                }
            }
        }

        if (index == (list.size() - 1)) {
            return "已到达底部";
        } else {
            WfModule module1 = list.get(index + 1);
            int sort = module.getOrderNum();
            int sort1 = module1.getOrderNum();

            module.setOrderNum(sort1);
            wfModuleMapper.updateByPrimaryKeySelective(module);

            module1.setOrderNum(sort);
            wfModuleMapper.updateByPrimaryKeySelective(module1);
        }

        return "下移成功";
    }
}
